package com.wing.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.purchase.dao.PurchaseQuotationDetailDao;
import com.wing.purchase.model.entity.PurchaseQuotationDetail;
import com.wing.purchase.model.entity.PurchaseRequisitionsDetail;
import com.wing.purchase.model.request.PurchaseQuotationDetailForm;
import com.wing.purchase.model.response.PurchaseQuotationDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 报价单明细-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class PurchaseQuotationDetailServiceImpl extends ServiceImpl<PurchaseQuotationDetailDao, PurchaseQuotationDetail> implements PurchaseQuotationDetailService {

    @Autowired
    private PurchaseRequisitionsDetailService purchaseRequisitionsDetailService;

    @Override
    public int add(PurchaseQuotationDetailForm purchaseQuotationDetailForm) {

        //校验

        PurchaseQuotationDetail purchaseQuotationDetail =new PurchaseQuotationDetail();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(PurchaseQuotationDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(purchaseQuotationDetailForm, purchaseQuotationDetail, copyOptions);

        return baseMapper.insert(purchaseQuotationDetail);
    }

    @Override
    public List<PurchaseQuotationDetailVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<PurchaseQuotationDetailVO> pageList4app(Page page, Map<String, Object> params) {
        List<PurchaseQuotationDetailVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public PurchaseQuotationDetailVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public int allocationPurchases(Long id, BigDecimal allocationPurchases) {
        PurchaseQuotationDetail purchaseQuotationDetail = baseMapper.selectById(id);
        if(purchaseQuotationDetail.getSupply().compareTo(allocationPurchases) < 0){
            throw new BusinessException(300, "分配量不能大于可供应量");
        }
        purchaseQuotationDetail.setAllocationPurchases(allocationPurchases);
        purchaseQuotationDetail.setHasAssign(true);
        BigDecimal taxPrice = purchaseQuotationDetail.getOfferTaxPrice();
        BigDecimal totalAmount = taxPrice.multiply(allocationPurchases);
        BigDecimal amount = allocationPurchases.multiply(taxPrice).multiply(new BigDecimal(100)).divide(purchaseQuotationDetail.getRate().add(new BigDecimal(100)), 2, BigDecimal.ROUND_HALF_UP);
        purchaseQuotationDetail.setRateAmount(totalAmount.subtract(amount));//税额
        purchaseQuotationDetail.setAmount(amount);//金额
        purchaseQuotationDetail.setTotalAmount(totalAmount);//总金额
        return baseMapper.updateById(purchaseQuotationDetail);
    }

    @Override
    public int updIsGenerateOrder(List<Long> quotationDetailIds) {
        return baseMapper.updIsGenerateOrder(quotationDetailIds);
    }
}
